package com.wxb.plugin.core.parser;

import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.wxb.plugin.core.gen.StringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wxb
 * @createTime 2025/2/27 16:07
 */
public abstract class Parser {

    private static Pattern p = Pattern.compile("(?<=\").*(?=\")");



    public static String getText(PsiAnnotation api, String... attributes) {
        return getText(api, true, attributes);
    }
    public static String getText(PsiAnnotation api, boolean match, String... attributes) {
        if (api == null) {
            return null;
        }
        for (String attribute : attributes) {

            PsiAnnotationMemberValue value = api.findAttributeValue(attribute);
            if(match){

                String text = value == null ? "" : match(value.getText());
                if(text != null && !text.trim().isEmpty()){
                    return text;
                }
            }else {
                if(value != null && StringUtil.isNotBlank(value.getText())){
                    return value.getText();
                }
            }

        }
        return "";
    }

    public static String match(String s) {
        Matcher matcher = p.matcher(s);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
}
